package cc.linker.steplink.crm.merchant.controller.acs;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchant.service.acs.WithdrawService;
import cc.linker.steplink.crm.response.GlobalException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


/**
 * 用户提现控制层
 */
@RestController
@RequestMapping("/withdraw")
public class WithdrawController {

    @Autowired
    private WithdrawService withdrawService;
    @Autowired
    private UserInfoUtil userInfoUtil;

    /**
     * 这一步为用户进行提现步骤前，先查看当前账户金额多少
     *
     * @param session
     * @return
     * @throws GlobalException
     */
    @RequestMapping("/checkmoney")
    public Map<String, String> checkMoney(HttpServletRequest request) throws GlobalException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        //查询权限认证中的用户casUserId
        String casUserId = userinfo.getAdminId();
        Map<String, String> maps = null;
        if (!StringUtils.isEmpty(casUserId)) {
            String customerId = withdrawService.findCustomerId(casUserId);
            if (!StringUtils.isEmpty(customerId)) {
                maps = withdrawService.checkMoney(customerId);
            }
        }
        //可用余额
        String cutomerBalance = maps.get("cutomerBalance");
        //冻结金额
        String customerFreeze = maps.get("customerFreeze");
        //TODO
        return maps;
    }

    /**
     * 点击提现之后，检查是否开通提现账户
     *
     * @param paramMap
     */
    @RequestMapping("/applyaccount")
    public void applyAccount(@RequestBody Map<String, String> paramMap, HttpServletRequest request) throws GlobalException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String adminId = userinfo.getAdminId();
        if (!StringUtils.isEmpty(adminId)) {
            String customerId = withdrawService.findCustomerId(adminId);
            if (!StringUtils.isEmpty(customerId)) {
                //查询该用户提现账户开通状态
                String withdrawStatus = withdrawService.findBankStatus(customerId);
                if (!StringUtils.isEmpty(withdrawStatus)) {

                }
            }
        }

    }


    /**
     * 提现审核列表
     *
     * @return
     */
    @GetMapping("/withdrawlist")
    public PageInfo withDrawList(
            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
            @RequestParam(value = "name", defaultValue = "", required = false) String name,
            @RequestParam(value = "startTime", defaultValue = "", required = false) String startTime,
            @RequestParam(value = "endTime", defaultValue = "", required = false) String endTime,
            @RequestParam(value = "status", required = false) Integer status,
            HttpServletRequest request
    ) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        Boolean ifSale = userinfo.getSaler();
        if (ifSale) {
            throw GlobalException.build("当前账号暂无权限！");
        }
        Map<String, Object> params = new HashMap<>();
        params.put("pageIndex", pageIndex);
        params.put("pageSize", pageSize);
        params.put("name", name);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("orgId", orgId);
        params.put("status", status);
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(withdrawService.withDrawList(params));
    }

    /**
     * 导出提现审核列表
     *
     * @return
     */
    @GetMapping("/exportwithdrawlist")
    public void exportWithDrawList(
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "name", defaultValue = "", required = false) String name,
            @RequestParam(value = "startTime", defaultValue = "", required = false) String startTime,
            @RequestParam(value = "endTime", defaultValue = "", required = false) String endTime,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        Boolean ifSale = userinfo.getSaler();
        if (ifSale) {
            throw GlobalException.build("当前账号暂无权限！");
        }
        Map<String, Object> params = new HashMap<>();
        params.put("orgId", orgId);
        params.put("status", status);
        params.put("name", name);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        withdrawService.export(params, response);
    }

    /**
     * 审核操作
     *
     * @param session
     * @param params
     * @throws Exception
     */
    @PostMapping("/checkwithdraw")
    public void checkWithDraw(
            HttpServletRequest request,
            @RequestBody Map<String, Object> params
    ) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        Boolean ifSale = userinfo.getSaler();
        if (ifSale) {
            throw GlobalException.build("当前账号暂无权限！");
        }
        params.put("orgId", orgId);
        withdrawService.checkWithDraw(params);
    }

    /**
     * 提现信息
     */
    @GetMapping("/withdrawInfo")
    public Map<String, Object> withdrawInfo(HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        return withdrawService.getWithdrawInfo(userinfo);
    }

}
